//
//  ECMaterailManageFieldAddViewController.m
//  ECERP.CRM
//
//  Created by huanggulong on 2018/11/22.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMaterailManageFieldAddViewController.h"
#import "ECMemberAddTableViewCell.h"
@interface ECMaterailManageFieldAddViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSArray<NSMutableDictionary*> * dataArr;

@end

@implementation ECMaterailManageFieldAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation];
    [self.view addSubview:self.tableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44 * PX_RELATIVE375;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ECMemberAddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableDictionary * dic = self.dataArr[indexPath.row];
    cell.isNeed = [[dic objectForKey:@"isNeed"] boolValue];
    cell.cellStyle = [[dic objectForKey:@"style"] integerValue];
    cell.title = [dic objectForKey:@"title"];
    switch (cell.cellStyle) {
        case ECMemberAddCellStyleTxt:
        {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailTF.placeholder = dic[@"placeholder"];
            }else{
                cell.detailTF.text = dic[@"value"];
            }
        }
            break;
        case ECMemberAddCellStyleMore: {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailLB.text = dic[@"placeholder"];
                cell.detailLB.textColor = ECCOLOR(0xcccccc);
            }else{
                cell.detailLB.text = dic[@"value"];
                cell.detailLB.textColor = ECCOLOR(0x333333);
            }
        }
            break;
        case ECMemberAddCellStyleSwitch:{
            if ([dic[@"value"] boolValue]) {
                cell.isON = YES;
            }else {
                cell.isON = NO;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

#pragma mark - event
- (IBAction)rightClick:(id)sender{
    NSLog(@"%s",__FUNCTION__);
}

#pragma mark - private
- (void)setNavigation{
    self.navigationItem.title = [NSString stringWithFormat:@"新增%@",[self fieldName]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick:)];
    
}

- (NSString *)fieldName{
    NSString * title = @"";
    switch (self.fieldType) {
        case ECMaterailManageFieldTypeUnit1:
        {
            title = @"商品单位";
            break;
        }
        case ECMaterailManageFieldTypeUnit2:{
            title = @"商品单位";
            break;
        }case ECMaterailManageFieldTypeBrand:{
            title = @"品牌";
            break;
        }case ECMaterailManageFieldTypeStore:{
            title = @"存储方式";
            break;
        }case ECMaterailManageFieldTypeTaxRate:{
            title = @"税率";
            break;
        }case ECMaterailManageFieldTypeSortZone:{
            title = @"分拣区域";
            break;
        }
        default:
            break;
    }
    return title;
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 27 * PX_RELATIVE375;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        [_tableView registerClass:[ECMemberAddTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSArray<NSMutableDictionary *> *)dataArr{
    if (_dataArr == nil) {
        _dataArr = @[@{@"title":[NSString stringWithFormat:@"%@ *",[self fieldName]],@"isNeed":@(YES),@"placeholder":[NSString stringWithFormat:@"请输入%@",[self fieldName]],@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                     @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                     @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy];
    }
    return _dataArr;
}

@end
